﻿using System.Collections.Generic;
using System.Linq;
using CSUtils;
using DG.Tweening;
using TripleJump;
using U3dUtils;
using UI00;
using UnityEngine;
using UnityEngine.UI;

namespace G02.TripleJump.UI {
    public class UI_GameEndRanking : UIBase {
        public static string prefabPath;

        protected override string _GetUIPrefabPath() {
            if (prefabPath != null) {
                return prefabPath;
            }
            return "TripleJump/UI/UI_GameEndRanking/UI_GameEndRanking.prefab";
        }

        public static Transform parent;

        protected override Transform _GetUIParent() {
            if (parent != null) {
                return parent;
            }
            return UIBase.uiLayer3_ui;
        }

        C_EndView endView = new C_EndView();
        C_DeathView deathView = new C_DeathView();

        protected override void _Init() {
            endView._Init(GetControl<RectTransform>("endView"), this);
            deathView._Init(GetControl<RectTransform>("deathView"), this);
        }

        float firstPlayerDistance;

        public void Open(bool firstPlayerIsDefEnd, TripleJump_FristRoleFly[] roleArray, float firstPlayerDistance) {
            m_rootRt.gameObject.SetActive(true);
        }

        protected override void _Open(Dictionary<string, object> paraDic) {
            var firstPlayerIsDefEnd = paraDic.GetBool("firstPlayerIsDefEnd");
            var roleArray = paraDic.GetObj<TripleJump_FristRoleFly[]>("roleArray");
            firstPlayerDistance = paraDic.GetFloat("firstPlayerDistance");
            if (firstPlayerIsDefEnd) {
                endView.Open(roleArray);
                deathView.Close();
            } else {
                endView.Close();
                deathView.Open();
            }
        }

        public class C_EndView {
            RectTransform root;
            Text title;
            Button comfirmButton;
            RectTransform cardPrefab;
            C_Card[] cards = new C_Card[8];
            RectTransform cardsParent;
            UI_GameEndRanking father;

            public void _Init(RectTransform root, UI_GameEndRanking father) {
                this.root = root;
                this.father = father;
                title = father.GetControl<Text>("title", root);
                comfirmButton = father.GetControl<Button>("comfirmButton", root);
                cardPrefab = father.GetControl<RectTransform>("cardPrefab", root);
                cardPrefab.gameObject.SetActive(false);
                cardsParent = new GameObject("cardsParent", typeof(RectTransform)).GetComponent<RectTransform>(); // RectTransform();
                cardsParent.SetParent(cardPrefab.parent);
                cardsParent.localPosition = Vector3.zero;
                for (int i = 0; i < cards.Length; i++) {
                    RectTransform rect = RectTransform.Instantiate(cardPrefab);
                    rect.SetParent(cardsParent);
                    rect.localPosition = new Vector3(cardPrefab.localPosition.x, cardPrefab.localPosition.y - i * (cardPrefab.sizeDelta.y + 5), 0);
                    rect.gameObject.SetActive(true);
                    C_Card nn = new C_Card();
                    nn._Init(rect);
                    cards[i] = nn;
                }
                comfirmButton.onClick.AddListener(delegate() {
                    UI_Checkout.Open(UI_Checkout.ERewardType.e01_Checkout, (int)father.firstPlayerDistance);
                    father.Close();
                });
            }

            float screenRatio;

            public void Open(TripleJump_FristRoleFly[] roleArray) {
                root.gameObject.SetActive(true);

                roleArray = roleArray.OrderByDescending(n => n.currFlyDistance).ToArray();
                for (int i = 0; i < cards.Length; i++) {
                    if (i < roleArray.Length) {
                        cards[i].name.text = roleArray[i].para.playerName;
                        cards[i].name.color = roleArray[i].para.playerNameColor;
                        cards[i].ranking.text = string.Format("{0} th", i + 1);
                        cards[i].ranking.color = roleArray[i].para.playerNameBackColor;

                        cards[i].distance.text = ((int)(roleArray[i].currFlyDistance + 1)) + "m";
                        cards[i].distance.color = roleArray[i].para.playerNameColor;

                        cards[i].headPortrait.color = roleArray[i].para.playerNameBackColor;
                        if (TripleJump_FristRoleFly.CPara.firstRoleId == roleArray[i].id) {
                            cards[i].root.color = Color.green.SetColor("7E7900aa");
                        } else {
                            cards[i].root.color = Color.green.SetColor("000000aa");
                        }
                    } else {
                        cards[i].name.text = "姓名未知";
                        //cards[i].name.color = roleArray[i].parameter.playerNameColor;
                        cards[i].ranking.text = "排名未知";
                        //cards[i].ranking.color = roleArray[i].parameter.playerNameColor;

                        cards[i].distance.text = "距离未知";
                        //cards[i].distance.color = roleArray[i].parameter.playerNameColor;
                    }

                    Vector3 localPosition1 = new Vector3(cardPrefab.localPosition.x, cardPrefab.localPosition.y - i * (cardPrefab.sizeDelta.y + 45), 0);
                    cards[i].root.rectTransform.localPosition = localPosition1 - Vector3.right * Screen.width;
                    int ii = i;
                    CDelayMain.Delay(i * 0.05f).SetPauseEnd("").onCallback = delegate {
                        cards[ii].root.rectTransform.DOLocalMove(localPosition1, 0.8f).SetEase(Ease.OutBack);
                    };
                }
            }

            public void Close() {
                root.gameObject.SetActive(false);
            }

            internal class C_Card {
                internal Image root, headPortrait;
                internal Text name, distance, ranking;

                public void _Init(RectTransform root) {
                    this.root = root.GetComponent<Image>();
                    headPortrait = root.Find("headPortrait").GetComponent<Image>();
                    name = root.Find("name").GetComponent<Text>();
                    distance = root.Find("distance").GetComponent<Text>();
                    ranking = root.Find("ranking").GetComponent<Text>();
                }
            }
        }
        public class C_DeathView {
            RectTransform root;
            Text title;
            Button comfirmButton;
            UI_GameEndRanking father;

            public void _Init(RectTransform root, UI_GameEndRanking father) {
                this.root = root;
                this.father = father;
                title = father.GetControl<Text>("title", root);
                comfirmButton = father.GetControl<Button>("comfirmButton", root);
                comfirmButton.onClick.AddListener(delegate() {
                    UI_Checkout.Open(UI_Checkout.ERewardType.e01_Checkout, (int)father.firstPlayerDistance);
                    father.Close();
                });
            }

            public void Open() {
                root.gameObject.SetActive(true);
            }

            public void Close() {
                root.gameObject.SetActive(false);
            }
        }
    }
}
